<template>
  <!-- 当点击 取消按钮 时，关闭弹窗 -->
  <div>
    <el-dialog
      :title="$t('mesUniqueCodePrinting.reprint')"
      :visible.sync="visible"
      width="60%"
      top="4vh"
      :append-to-body="true"
      @close="clearDialog()"
      v-loading="loding"
      :close-on-click-modal="false"
    >
      <ele-form v-model="dialogfrom" v-bind="formConfig" ref="form"> </ele-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="clearDialog()">{{ $t("common.cancel") }}</el-button>
        <el-button
          v-if="dialogfrom.type == 1"
          type="primary"
          @click="handleSave"
          >{{ $t("common.save") }}</el-button
        >
        <el-button
          v-else
          :disabled="!dialogfrom.quantity"
          type="primary"
          @click="handleRequest"
          >{{ $i18n.t("mesUniqueCodePrinting.reprint") }}</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  barcodePrintingList2,
  printTemplateInfo,
  selectListByConfigurationCode,
} from "@/api/print/mesProdOrder";
import BarcodePrint from "@/components/barcodePrint/barcodePrint";
import { setPrintData } from "@/utils/judge.js";
import {
  reprintSel,
  reprintSelList,
  mesProdBarcodeReprintList,
  getPrintingTimes,
} from "@/api/print/mesProdBarcodePrint";
import qs from "qs";
export default {
  name: "barcodePrintingAddModel",
  watch: {
    visible(val, oldVal) {
      if (!val) {
        this.$nextTick(() => {
          this.$refs.form.resetForm();
        });
        this.barcodeInfo = {};
      }
    },
  },
  props: {
    title: {
      type: String,
      default: "",
    },
  },
  components: { BarcodePrint },
  data() {
    let that = this;
    return {
      innerVisible: false,
      loding: false,
      printRow: {},
      form: { printingTimes: 0 },
      loding: false,
      time: 0,
      formConfig: {
        span: 24,
        isShowBackBtn: false,
        isShowSubmitBtn: false,
        labelPosition: "right",
        formDesc: {
          orderCode: {
            type: "input",
            label: $i18n.t("mesUniqueCodePrinting.workOrderCode"),
            layout: 12,
            required: true,
            disabled: true,
            attrs: {
              stepStrictly: true,
              controlsPosition: "right",
              min: 1,
            },
          },
          prodCode: {
            type: "input",
            label: $i18n.t("mesUniqueCodePrinting.materialCode"),
            layout: 12,
            required: true,
            disabled: true,
          },
          prodName: {
            type: "input",
            label: $i18n.t("mesUniqueCodePrinting.materialName"),
            layout: 12,
            required: true,
            disabled: true,
          },
          quantity: {
            type: "input",
            label: $i18n.t("mesUniqueCodePrinting.barcodeQty"),
            layout: 12,
            disabled: true,
            required: true,
          },
          startBarCode: {
            type: "input",
            label: $i18n.t("mesUniqueCodePrinting.startBarCode"),
            layout: 24,
            required: true,
          },
          endBarCode: {
            type: "input",
            label: $i18n.t("mesUniqueCodePrinting.endBarCode"),
            layout: 24,
            required: true,
          },
          // printingTimes: {
          //   type: 'input',
          //   label: $i18n.t('mesProdOtherCode.printingTimes'),
          //   layout: 24,
          //   disabled: true
          // }
        },
      },
      barcodeInfo: {},
      dialogfrom: {},
      visible: false,
      loading: false,
      list: [],
      arrBarcodePrintingList2: [],
    };
  },
  methods: {
    // 初始化
    init(params) {
      console.log("11params", params);
      this.dialogfrom = params;
      this.barcodeInfo = params;
      this.visible = true;
      this.dialogfrom.printQuantity = this.dialogfrom.quantity; //条码数量=计划数量
      this.reprintSel();
    },

    /**
     * @author bing
     * @since 2022-11-17 14:44:33
     *  获取补打信息
     */
    async reprintSel() {
      const res = await reprintSel({ orderCode: this.dialogfrom.orderCode });
      this.$nextTick(() => {
        this.dialogfrom = {
          startBarCode: "",
          endBarCode: "",
          ...this.dialogfrom,
          ...res.data.result,
        };
        // if (this.dialogfrom.type == 1) {
        //   // this.$set(this.dialogfrom, "startBarCode", "");
        //   this.dialogfrom.startBarCode = "";
        //   this.dialogfrom.endBarCode = "";
        // }
      });
    },
    clearFormDialog() {
      this.innerVisible = false;
      this.form.printingTimes = 0;
    },

    clearDialog() {
      this.visible = false;
      this.$emit("refreshList");
    },
    // 点击打印按钮
    async handleRequest() {
      // let res = await getPrintingTimes({ goodsCode: this.dialogfrom.prodCode })
      // if (res.data.result && res.data.result.frequency) {
      //   this.dialogfrom.printingTimes = res.data.result.frequency
      // }
      this.$refs.form.handleSubmitForm();
      // if (this.dialogfrom.printingTimes == 0) {
      //   this.$message.warning($i18n.t('printBoxCode.unableToPrint'))
      //   return
      // }
      this.$refs.form.validate().then((valid) => {
        const { orderCode, startBarCode, endBarCode } = this.dialogfrom;
        const data = {
          orderCode,
          startBarCode,
          endBarCode,
        };
        reprintSelList(data).then((res) => {
          if (res.data.success) {
            let list = res.data.result;

            let { barcodePrintTempCode, barcodePrintTemp } = this.barcodeInfo;
            setPrintData(
              {
                code: barcodePrintTempCode,
                name: barcodePrintTemp,
                status: 1,
              },
              list
            );
            this.clearDialog();
            this.$router.push("/splitPrint");
          }
        });
      });
    },

    // 点击保存按钮
    handleSave() {
      this.$refs.form.validate().then((valid) => {
        const { orderCode, startBarCode, endBarCode } = this.dialogfrom;
        const data = {
          orderCode,
          startBarcode: startBarCode,
          endBarcode: endBarCode,
        };
        mesProdBarcodeReprintList(data).then((res) => {
          this.visible = false;
          this.$parent.$refs.repairPrintTables.refresh();
          this.$message.success($i18n.t("common.submitSuccess"));
        });
      });
    },
  },
  async created() {
    let list = await barcodePrintingList2();
    this.arrBarcodePrintingList2 = list.data.result || [];
  },
};
</script>
